package com.ddlx.test.binder.activity;

import java.lang.ref.SoftReference;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.ddlx.test.binder.R;
import com.ddlx.test.binder.aidl.IDataSender;
import android.content.Intent;
import android.content.pm.IPackageStatsObserver;
import android.content.pm.PackageManager;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

public enum ClickHandler implements OnClickListener {
	INTENT_GREATER1016KB(R.id.intent_greater1016kb) {
		@Override public void process() {
			if (!checkDataSenderInited() ) {
				return;
			}
			Intent i = new Intent(activityReference.get(), IntentActivity.class);
			i.putExtra("test", new byte[1016*1024]);
			updateText("intent:"+i);setHintInfo("intent 传递的数据大小小于508KB");
			activityReference.get().startActivity(i);
		}
	},
	
	INTENT_LESS1016KB(R.id.intent_less1016kb) {
		@Override public void process() {
			if (!checkDataSenderInited() ) {
				return;
			}
			Intent i = new Intent(activityReference.get(), IntentActivity.class);
			i.putExtra("test", new byte[506*1024]);
			updateText("intent:"+i);
			setHintInfo("intent 传递的数据大小小于508KB");
			activityReference.get().startActivity(i);
		}
	},
	
	INTENT_2M_IMAGE(R.id.intent_2m_image) {
		@Override public void process() {
			if (!checkDataSenderInited() ) {
				return;
			}
			Intent i = new Intent(activityReference.get(), IntentActivity.class);
			i.putExtra("test", BitmapFactory.decodeResource(activityReference.get().getResources(), R.drawable.test_2m_image));
			updateText("intent:"+i);
			updateText("数据太大传输不过去。可以从logcat中看到如下日志信息：\n!!! FAILED BINDER TRANSACTION !!!");
			setHintInfo("intent不支持文件传输");
			activityReference.get().startActivity(i);
		}
	},
	
	SLEEP_BINDER_THREADS(R.id.sleep_binder_threads) {
		@Override public void process() {
			if (!checkDataSenderInited() ) {
				return;
			}
			updateText("挂起binder线程20秒");
			final PackageManager packageManager = activityReference.get().getPackageManager();
			try {
				Method method = packageManager.getClass().getMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class );
				
				final int maxCount = 16;
				
				for (int i = 0; i < maxCount; i++) {
					final int a = i;
					try {
						method.invoke( packageManager, "com.ddlx.test.binder", new IPackageStatsObserver.Stub(){
							@Override
							public void onGetStatsCompleted(android.content.pm.PackageStats arg0, boolean arg1){
								updateText("挂起线程:"+Thread.currentThread());
								Log.d("ddlx","挂起线程:"+Thread.currentThread());
								try {
									Thread.sleep(20*1000);
								} catch (InterruptedException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
								updateText("结束挂起         :"+Thread.currentThread());
								Log.d("ddlx","结束挂起         :"+Thread.currentThread());
							}
						});
					} catch (InvocationTargetException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	};
	
	abstract public void process();
	
	@Override public void onClick(View v) {
		initTextInfo();
		process();
	}
	protected int id;
	protected SoftReference<MainActivity> activityReference;
	ClickHandler(int id ) {
		this.id = id;
	}
	
	protected void setHintInfo( String info ) {
		MainActivity activity = activityReference.get();
		if ( activity != null ) {
			activity.updateHintHander.appendTextln(info);
		}
	}
	
	protected boolean checkBinderConnOk() {
		MainActivity activity = activityReference.get();
		if ( activity != null ) {
			return activity.checkBinderConnOk();
		}
		return false;
	}
	
	protected boolean checkDataSenderInited() {
		MainActivity activity = activityReference.get();
		if ( activity != null ) {
			return activity.checkDataSenderInited();
		}
		return false;
	}
	
	protected void updateText( String infoString ) {
		MainActivity activity = activityReference.get();
		if ( activity != null ) {
			activity.updateTextViewHander.appendTextln(infoString);
		}
	}
	
	protected void initTextInfo() {
		MainActivity activity = activityReference.get();
		if ( activity != null ) {
			activity.initTextInfo(id);
		}
	}
	protected IDataSender getDataSender() {
		MainActivity activity = activityReference.get();
		if ( activity != null ) {
			return activity.getDataSender();
		}
		return null;
	}
	
	public int getId( MainActivity activity ) {

		activityReference = new SoftReference<MainActivity>(activity);
		return id;
	}
}
